<?php
/**
 * Created by PhpStorm.
 * User: Lyq
 * Date: 2018/8/22
 * Time: 上午8:21
 */
namespace app\admin\controller;

use think\Exception;
use think\Db;

class Duties extends Common
{

    public function index()
    {
        $this->templateTitle('职务列表');

        //判断条件
        $where=[];

        //搜索条件
        $condition = request()->param("condition");
        $this->assign("condition", $condition);
        if ($condition)
        {
            $where["title"] = ["LIKE", "%$condition%"];
        }

        $list = Db::name("Duties")
            ->where($where)
            ->order("level ASC,display_order DESC")
            ->paginate(15);
        $this->assign("list", $list);

        return $this->fetch();
    }

    //添加行业
    public function add()
    {
        if (request()->isGet())
        {
            $this->layoutModal();

            return $this->fetch();
        }

        if (request()->isAjax())
        {
            //创建数据
            $data = array(
                "level"=>2,//目前职务只能加级别为2
                "create_time" => now_datetime(),
                "update_time" => now_datetime(),
            );

            //新闻名称
            $data["title"] = request()->param("title");
            if(empty($data['title'])){
                return $this->ajaxError('请输入职务名称');
            }

            //行业排序
            $data["display_order"] = request()->param("order");
            //是否显示
//            $data["is_visible"] = request()->param("visible");

            try
            {
                $add = Db::name("duties")->insert($data);
                if($add){
                    $this->ajaxSuccess('职务添加成功');
                }else{
                    $this->ajaxSuccess('职务添加失败');
                }

            }
            catch (Exception $exception)
            {
                $this->ajaxError($exception->getMessage());
            }
        }
    }

    //编辑新闻
    public function edit()
    {
        //新闻ID
        $id = request()->param("id");

        if (request()->isGet())
        {
            $this->layoutModal();

            //获取新闻信息
            $info = Db::name("duties")
                ->where("id", $id)
                ->find();
            $this->assign("info", $info);

            return $this->fetch();
        }

        if (request()->isAjax())
        {
            //
            //创建数据
            $data = array(
                "update_time" => now_datetime(),
            );


            $data["title"] = request()->param("title");
            if(empty($data['title'])){
                return $this->ajaxError('请输入职务名称');
            }

            $data["display_order"] = request()->param("order");
            //是否显示
//            $data["is_visible"] = request()->param("visible");

            try
            {
                //更新新闻
                $save = Db::name("duties")
                    ->where("id", $id)
                    ->update($data);
                if($save){
                    $this->ajaxSuccess('职务编辑成功');
                }else{
                    $this->ajaxError('职务编辑失败');
                }

            }
            catch (Exception $exception)
            {
                $this->ajaxError($exception->getMessage());
            }
        }
    }


    public function remove()
    {
        if (request()->isAjax())
        {
            //商会ID
            $id = request()->param("id");
            $duties = Db::name("duties")
                ->where("id", $id)
                ->find();
            if($duties['level']!=2){
                return $this->ajaxError('此职务禁止删除！');
            }
            try
            {
                //更新新闻
                $del = Db::name("duties")
                    ->where("id", $id)
                    ->delete();
                if($del){
                    $this->ajaxSuccess('职务删除成功');
                }else{
                    $this->ajaxSuccess('职务删除失败');
                }
            }
            catch (Exception $exception)
            {
                $this->ajaxError($exception->getMessage());
            }
        }
    }


}